package com.huike.web.controller.contract;

import com.huike.clues.service.TbClueService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysUserDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.contract.domain.TbContract;
import com.huike.contract.domain.dto.TbContractDTO;
import com.huike.contract.service.TbContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合同Controller
 */
@RestController
@RequestMapping("/contract")
@Api(tags = "合同管理相关接口")
public class TbContractController extends BaseController {
    @Resource
    private TbContractService tbContractService;

    @Resource
    private TbClueService tbClueService;

    @ApiOperation("查询合同列表")
    @PreAuthorize("@ss.hasPermi('contract:contract:list')")
    @GetMapping("/list")
    public TableDataInfo<List<TbContract>> list(TbContract tbContract) {
        //调用服务层查询合同列表
        List<TbContract> list = tbContractService.selectTbContractList(tbContract);
        //返回分页数据
        return getDataTablePage(list);
    }

    @ApiOperation("新增合同")
    @PreAuthorize("@ss.hasPermi('contract:contract:add')")
    @PostMapping
    public AjaxResult<Integer> save(@RequestBody TbContract tbContract) {
        //检查手机号是否已存在
        if (!tbClueService.checkCluePhoneExis(tbContract.getPhone())) {
            return error("已存在对应手机号的线索");
        }
        //调用服务层新增合同
        return toAjax(tbContractService.insertTbContract(tbContract));
    }

    @ApiOperation("获取合同信息1")
    @PreAuthorize("@ss.hasPermi('contract:contract:detail')")
    @GetMapping("/detail/{id}")
    public AjaxResult<TbContract> detail(@PathVariable("id") Long id) {
        //调用服务层查询合同信息
        return AjaxResult.success(tbContractService.selectTbContractById(id));
    }

    @ApiOperation("获取合同信息2")
    @PreAuthorize("@ss.hasPermi('contract:contract:detail')")
    @GetMapping("/{id}")
    public AjaxResult<TbContract> detailById(@PathVariable("id") Long id) {
        //调用服务层查询合同信息
        return AjaxResult.success(tbContractService.selectTbContractById(id));
    }

    @ApiOperation("商机转合同")
    @PreAuthorize("@ss.hasPermi('contract:contract:change')")
    @PutMapping("/changeContract/{id}")
    public AjaxResult<Integer> changeContract(@PathVariable Long id, @RequestBody TbContract tbContract) {
        //调用服务层将商机转换为合同
        return toAjax(tbContractService.changeContract(id, tbContract));
    }

    @ApiOperation("修改合同")
    @PreAuthorize("@ss.hasPermi('contract:contract:edit')")
    @PutMapping
    public AjaxResult<Integer> edit(@RequestBody TbContract tbContract) {
        //调用服务层修改合同
        return toAjax(tbContractService.updateTbContract(tbContract));
    }
}
